package com.example.mediamoudle.utlis;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.*;


/**
 * Created by  you
 * Date: 2022/7/5
 * Time: 9:46
 */
public class DefaultItemTouchHelper extends VideoItemTouchHelper {

    private DefaultItemTouchHelpCallback itemTouchHelpCallback;

    /**
     * Creates an ItemTouchHelper that will work with the given Callback.
     * <p>
     * You can attach ItemTouchHelper to a RecyclerView via
     * {@link #attachToRecyclerView(RecyclerView)}. Upon attaching, it will add an item decoration,
     * an onItemTouchListener and a Child attach / detach listener to the RecyclerView.
     */
    public DefaultItemTouchHelper(DefaultItemTouchHelpCallback.OnItemTouchCallbackListener onItemTouchCallbackListener) {
        super(new DefaultItemTouchHelpCallback(onItemTouchCallbackListener));
        itemTouchHelpCallback = (DefaultItemTouchHelpCallback) getCallback();
    }

    /**
     * 设置是否可以被拖拽
     *
     * @param canDrag 是true，否false
     */
    public void setDragEnable(boolean canDrag) {
        itemTouchHelpCallback.setDragEnable(canDrag);
    }

    public static class DefaultItemTouchHelpCallback extends Callback {
        /**
         * Item操作的回调
         */
        private OnItemTouchCallbackListener onItemTouchCallbackListener;

        /**
         * 是否可以拖拽
         */
        private boolean isCanDrag = false;

        public DefaultItemTouchHelpCallback(OnItemTouchCallbackListener onItemTouchCallbackListener) {
            this.onItemTouchCallbackListener = onItemTouchCallbackListener;
        }

        /**
         * 设置是否可以被拖拽
         *
         * @param canDrag 是true，否false
         */
        public void setDragEnable(boolean canDrag) {
            isCanDrag = canDrag;
        }

        @Override
        public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
            if (actionState == ItemTouchHelper.ACTION_STATE_DRAG && onItemTouchCallbackListener != null) {
                onItemTouchCallbackListener.onItemDragStart(viewHolder, viewHolder.getAdapterPosition());
            }
            super.onSelectedChanged(viewHolder, actionState);
        }


        @Override
        public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            if (onItemTouchCallbackListener != null) {
                onItemTouchCallbackListener.onItemDragEnd(viewHolder, viewHolder.getAdapterPosition());
            }
        }

        /**
         * 当Item被长按的时候是否可以被拖拽
         *
         * @return
         */
        @Override
        public boolean isLongPressDragEnabled() {
            return isCanDrag;
        }

        /**
         * 当用户拖拽或者滑动Item的时候需要我们告诉系统滑动或者拖拽的方向
         *
         * @param recyclerView
         * @param viewHolder
         * @return
         */
        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

            if (layoutManager instanceof GridLayoutManager) {// GridLayoutManager
                // flag如果值是0，相当于这个功能被关闭
                int dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                int swipeFlag = 0;
                // create make
                if (onItemTouchCallbackListener != null) {
                    dragFlag = onItemTouchCallbackListener.getDragFlag(recyclerView, viewHolder);
                }
                return makeMovementFlags(dragFlag, swipeFlag);
            } else if (layoutManager instanceof LinearLayoutManager) {// linearLayoutManager
                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                int orientation = linearLayoutManager.getOrientation();

                int dragFlag = 0;
                int swipeFlag = 0;

                // 为了方便理解，相当于分为横着的ListView和竖着的ListView
                if (orientation == LinearLayoutManager.HORIZONTAL) {// 如果是横向的布局
                    dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                } else if (orientation == LinearLayoutManager.VERTICAL) {// 如果是竖向的布局，相当于ListView
                    dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
                }

                if (onItemTouchCallbackListener != null) {
                    dragFlag = onItemTouchCallbackListener.getDragFlag(recyclerView, viewHolder);
                }
                return makeMovementFlags(dragFlag, swipeFlag);
            }
            return 0;
        }

        /**
         * 当Item被拖拽的时候被回调
         *
         * @param recyclerView     recyclerView
         * @param srcViewHolder    拖拽的ViewHolder
         * @param targetViewHolder 目的地的viewHolder
         * @return
         */
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder srcViewHolder, RecyclerView.ViewHolder targetViewHolder) {
            if (onItemTouchCallbackListener != null) {
                return onItemTouchCallbackListener.onMove(srcViewHolder.getAdapterPosition(), targetViewHolder.getAdapterPosition());
            }
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

        }

        public interface OnItemTouchCallbackListener {

            /**
             * 当两个Item位置互换的时候被回调
             *
             * @param srcPosition    拖拽的item的position
             * @param targetPosition 目的地的Item的position
             * @return 开发者处理了操作应该返回true，开发者没有处理就返回false
             */
            boolean onMove(int srcPosition, int targetPosition);

            int getDragFlag(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder);

            void onItemDragStart(RecyclerView.ViewHolder viewHolder, int pos);

            void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int pos);
        }
    }
}

